package com.zlll.winner.controller.bbs;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.zlll.winner.business.model.bbs.BbsSubject;
import com.zlll.winner.business.service.bbs.IBbsSubjectCityService;
import com.zlll.winner.business.service.bbs.IBbsSubjectService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.enums.CommonEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 论坛专题管理
 */
@LoginUser
@RestController
@RequestMapping(value = "/bbs/subject")
public class BbsSubjectController extends BaseController {

    @Reference
    private IBbsSubjectService bbsSubjectService;

    @Reference
    private IBbsSubjectCityService bbsSubjectCityService;

    /**
     * 分页列表
     * @param params
     * @return
     */
    @PostMapping(value = "page")
    public Object page(@RequestBody JSONObject params){
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Assert.notNull(pageNo,"pageNo不能为空");
        Assert.notNull(pageSize,"pageSize不能为空");
        return bbsSubjectService.fetchSubjectPage(params);
    }

    /**
     * 保存
     * @param params
     * @return
     */
    @PostMapping(value = "save")
    public Object save(@RequestBody JSONObject params){
        JSONArray cityList = params.getJSONArray("cityList");
        BbsSubject bbsSubject = params.toJavaObject(BbsSubject.class);
        checkFields(bbsSubject, cityList);
        bbsSubject.setCreateTime(System.currentTimeMillis());
        bbsSubject.setCreateUser(getUser().getUserName());
        bbsSubject.setUpdateTime(bbsSubject.getCreateTime());
        bbsSubject.setUpdateUser(bbsSubject.getCreateUser());
        return bbsSubjectService.saveSubject(cityList,bbsSubject);
    }

    /**
     * 更新
     * @param params
     * @return
     */
    @PostMapping(value = "update")
    public Object update(@RequestBody JSONObject params){
        Object result;
        String actionType = params.getString("actionType");
        if(actionType != null && "updateStatus".equals(actionType) ){
            BbsSubject bbsSubject = params.toJavaObject(BbsSubject.class);
            Assert.notNull(bbsSubject.getId(),"话题ID不能为空");
            bbsSubject.setUpdateTime(System.currentTimeMillis());
            bbsSubject.setUpdateUser(getUser().getUserName());
            result = bbsSubjectService.updateById(bbsSubject);
        }else{
            JSONArray cityList = params.getJSONArray("cityList");
            BbsSubject bbsSubject = params.toJavaObject(BbsSubject.class);
            Assert.notNull(bbsSubject.getId(),"修改主题ID不能为空");
            checkFields(bbsSubject, cityList);
            bbsSubject.setUpdateTime(System.currentTimeMillis());
            bbsSubject.setUpdateUser(getUser().getUserName());
            result = bbsSubjectService.updateSubject(cityList, bbsSubject);
        }
        return result;
    }

    private void checkFields(BbsSubject bbsSubject, JSONArray cityList) {
        Assert.notNull(cityList,"投放城市不能为空");
        Assert.notNull(bbsSubject.getSubject(),"话题主题不能为空");
        Assert.notNull(bbsSubject.getBeginTime(),"话题开始不能为空");
        Assert.notNull(bbsSubject.getEndTime(),"话题结束不能为空");
        Assert.notNull(bbsSubject.getIntroduction(),"话题导语不能为空");
        Assert.notNull(bbsSubject.getPosition(),"话题位置不能为空");
        Assert.notNull(bbsSubject.getImage(),"话题配图不能为空");
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @PostMapping(value = "delete")
    public Object delete(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        BbsSubject updateObj = new BbsSubject();
        updateObj.setId(id);
        updateObj.setUpdateTime(System.currentTimeMillis());
        updateObj.setUpdateUser(getUser().getUserName());
        updateObj.setIsDel(CommonEnum.DelEnum.DEL.val());
        bbsSubjectService.updateById(updateObj);
        return updateObj;
    }

    /**
     * 详情
     * @param params
     * @return
     */
    @PostMapping(value = "details")
    public Object details(@RequestBody JSONObject params){
        Long id = params.getLong("id");
        Assert.notNull(id,"记录Id不能为空");
        JSONObject result  = new JSONObject();
        result.put("details",bbsSubjectService.getById(id));
        result.put("cityList",bbsSubjectCityService.getCityListBySubjectId(id));
        return result;
    }

}
